package streaming.test.rlm.call.video;

import java.awt.Component;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.media.CachingControl;
import javax.media.CachingControlEvent;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.Manager;
import javax.media.MediaEvent;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.RealizeCompleteEvent;
import javax.media.Time;
import javax.media.TransitionEvent;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;



public class VideoCallBad extends JFrame implements ControllerListener{

	Player player = null;

	Component playerControlPanel;
	Component playerVisual;
	Component playerProgress;

	MediaLocator mediaLocator = null;

	public VideoCallBad(){

	}

	public VideoCallBad(String url){
		
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			e1.printStackTrace();
		}
		setBounds(0 , 0, 320, 240);
		
		this.mediaLocator = new MediaLocator(url);

		
		try {

			player = Manager.createPlayer(mediaLocator);

			player.addControllerListener(this);
			System.out.println("Realizing");
			player.realize();

		} catch (NoPlayerException e) {
			System.out.println("No player");
			e.printStackTrace();

		} catch (IOException e) {
			System.out.println("IO error");
			e.printStackTrace();
		}
		
		
		setVisible(true);

	}

	@Override
	public void controllerUpdate(ControllerEvent e) {

//		if (player == null)
//			return;
		
		if (e instanceof TransitionEvent){
			TransitionEvent te = (TransitionEvent)e;
			System.out.println(te);
			
		}

		if (e instanceof RealizeCompleteEvent){

			if ( (playerVisual = player.getVisualComponent()) != null ){
				getContentPane().add("Center", playerVisual);
				System.out.println("added visual");
			}

			if( (playerControlPanel = player.getControlPanelComponent()) != null){
				getContentPane().add("South", playerControlPanel);
			}

			System.out.println("realized");
			validate();

		}
		
//		else if(e instanceof ){
//			
//		}

		else if (e instanceof CachingControlEvent){

			CachingControlEvent cce = (CachingControlEvent)e;
			CachingControl cc = cce.getCachingControl();
			long status = cc.getContentProgress();
			long length = cc.getContentLength();

			if(playerProgress == null)
				if( (playerProgress = cc.getProgressBarComponent()) != null){

					getContentPane().add("North", playerProgress);
					validate();
					
				}

			if(playerProgress != null){
				
				if(status == length){
					
					getContentPane().remove(playerProgress);
					playerProgress = null;
					validate();
					
				}
				
			}
		}
		
		else if (e instanceof EndOfMediaEvent){
			player.setMediaTime(new Time(0));
			player.start();
		}

	}

	
	public static void main(String[] args) {
//		if(args.length > 1){
//			System.out.println(args[1]);
//		}
		VideoCallBad call = new VideoCallBad("rtp://192.168.1.10:22222/video/1");
//		VideoCall call = new VideoCall("file:///J:/Raso/Videos/lokospot.mpg");
	}
}









